﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NetDon.WebControls;
using NetDon.OA.Interface;
using NetDon.Utilities;
using NetDon.OA.Interface.eLogistics;
using NetDon.OA.Interface.CRM;

namespace NetDon.OA.Portal.eLogisticsQuery
{
     [Navigation("报表查询", "查询汇总")]
    public partial class QueryTotalIssuing : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                txtReceivingOrderDateStart.Text =
                  DateTime.Now.AddDays(-7).ToString("yyyy-MM-dd");
                txtReceivingOrderDateEnd.Text =
              DateTime.Now.ToString("yyyy-MM-dd");
                this.ddlCustomer.InsertEmptyItem(ListEmptyType.Select);
               
            }
        }

        public void ddlCustomer_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(ddlCustomer.SelectedValue))
            {

                LoadGoodsShippedTypes(ddlCustomer.SelectedValue.ToGuid());
            }


        }

        private void LoadGoodsShippedTypes(Guid customerId)
        {
            this.ddlGoodsShippedTypeId.Items.Clear();
            this.ddlGoodsShippedTypeId.DataSource = ServiceContainer.GetService<ICustomerInfoService>().GetCustomerGoodsShippTypes(
               customerId, CurrentApplicationContext.CompanyId);
            this.ddlGoodsShippedTypeId.DataBind();
            ddlGoodsShippedTypeId.InsertEmptyItem(ListEmptyType.Select);
        }


        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

           
        }

        public void btnSearch_Click(object sender, EventArgs e)
        {
            var criteria = new IIssuingTotalOrder
            {

            };

            DateTime? dateStart = null, dateEnd = null;
            if (!string.IsNullOrEmpty(txtReceivingOrderDateStart.Text.Trim()))
            {
                dateStart = DateTime.Parse(txtReceivingOrderDateStart.Text.Trim());
            }
            if (!string.IsNullOrEmpty(txtReceivingOrderDateEnd.Text.Trim()))
            {
                dateEnd = DateTime.Parse(txtReceivingOrderDateEnd.Text.Trim());
            }
            if (dateStart.HasValue || dateEnd.HasValue)
            {
                criteria.TimeQuantum = new Range<DateTime?>(dateStart, dateEnd);
            }
            //客户
            if (!MiscUtilities.IsNullOrEmpty(ddlCustomer.SelectedValue.ToGuid()))
            {
                criteria.CustomerId = ddlCustomer.SelectedValue.ToGuid();
            }
            //承运货物
            if (!string.IsNullOrEmpty(ddlGoodsShippedTypeId.SelectedValue))
            {
                criteria.GoodsShippedTypeId = ddlGoodsShippedTypeId.SelectedValue.ToGuid();
            }
            //线路分类
            if (!MiscUtilities.IsNullOrEmpty(ddlRouteTypeId.SelectedValue.ToGuid()))
            {
                criteria.RouteTypeId = ddlRouteTypeId.SelectedValue.ToGuid();
            }
            //PageQueryAdapter1.PerformQuery();
            var service = ServiceContainer.GetService<IReportQueryService>();
            var dataTable = service.QueryIssuingTotalOrder(criteria, "IssuingTotalOrder");
            HttpContext.Current.Session["TotalEntrucking"] = GetSearchCriteria(true,null);
            HttpContext.Current.Session["TotalNonEntrucking"] = GetSearchCriteria(false, null);
            HttpContext.Current.Session["TotalShunted"] = GetSearchCriteria(null, true);
            HttpContext.Current.Session["TotalNonShunted"] = GetSearchCriteria(null, false);
            HttpContext.Current.Session["TotalTracked"] = GetSearchCriteriaTrack(true);
            HttpContext.Current.Session["TotalNonTracked"] = GetSearchCriteriaTrack(false);
            HttpContext.Current.Session["TotalReceived"] = GetSearchCriteriaReceived(true);
            HttpContext.Current.Session["TotalNonReceived"] = GetSearchCriteriaReceived(false);
            this.gridOrderTotalList.DataSource = dataTable;
            this.gridOrderTotalList.DataBind();
        }


        protected IssuingOrderSearchCriteria GetSearchCriteria(object isEntruck,object isShunted)
        {
            var criteria = new IssuingOrderSearchCriteria
            {

            };
            
            DateTime? dateStart = null, dateEnd = null;
            if (!string.IsNullOrEmpty(txtReceivingOrderDateStart.Text.Trim()))
            {
                dateStart = DateTime.Parse(txtReceivingOrderDateStart.Text.Trim());
            }
            if (!string.IsNullOrEmpty(txtReceivingOrderDateEnd.Text.Trim()))
            {
                dateEnd = DateTime.Parse(txtReceivingOrderDateEnd.Text.Trim());
            }
            if (dateStart.HasValue || dateEnd.HasValue)
            {
                criteria.ReceivingOrderDate = new Range<DateTime?>(dateStart, dateEnd);
            }
            //客户
            if (!MiscUtilities.IsNullOrEmpty(ddlCustomer.SelectedValue.ToGuid()))
            {
                criteria.CustomerId = ddlCustomer.SelectedValue.ToGuid();
            }
            //承运货物
            if (!string.IsNullOrEmpty(ddlGoodsShippedTypeId.SelectedValue))
            {
                criteria.GoodsShippedTypeId = ddlGoodsShippedTypeId.SelectedValue.ToGuid();
            }
            //线路分类
            if (!MiscUtilities.IsNullOrEmpty(ddlRouteTypeId.SelectedValue.ToGuid()))
            {
                criteria.RouteTypeId = ddlRouteTypeId.SelectedValue.ToGuid();
            }
            if (isEntruck != null)
            {
                criteria.IsEntruck = Convert.ToBoolean(isEntruck);
            }
            if (isShunted != null)
            {
                criteria.IsShunted = Convert.ToBoolean(isShunted);
            }
            

            
            return criteria;
        }

        protected IssuingOrderOnWayTrackSearchCriteria GetSearchCriteriaTrack(object isTracked)
        {
            var criteria = new IssuingOrderOnWayTrackSearchCriteria
            {

            };
          
            DateTime? dateStart = null, dateEnd = null;
            if (!string.IsNullOrEmpty(txtReceivingOrderDateStart.Text.Trim()))
            {
                dateStart = DateTime.Parse(txtReceivingOrderDateStart.Text.Trim());
            }
            if (!string.IsNullOrEmpty(txtReceivingOrderDateEnd.Text.Trim()))
            {
                dateEnd = DateTime.Parse(txtReceivingOrderDateEnd.Text.Trim());
            }
            if (dateStart.HasValue || dateEnd.HasValue)
            {
                criteria.ReceivingOrderDate = new Range<DateTime?>(dateStart, dateEnd);
            }
            //客户
            if (!MiscUtilities.IsNullOrEmpty(ddlCustomer.SelectedValue.ToGuid()))
            {
                criteria.CustomerId = ddlCustomer.SelectedValue.ToGuid();
            }
            //承运货物
            if (!string.IsNullOrEmpty(ddlGoodsShippedTypeId.SelectedValue))
            {
                criteria.GoodsShippedTypeId = ddlGoodsShippedTypeId.SelectedValue.ToGuid();
            }
            //线路分类
            if (!MiscUtilities.IsNullOrEmpty(ddlRouteTypeId.SelectedValue.ToGuid()))
            {
                criteria.RouteTypeId = ddlRouteTypeId.SelectedValue.ToGuid();
            }
            if (isTracked != null)
            {
                criteria.IsTracked = Convert.ToBoolean(isTracked);
            }
            return criteria;
        }

        protected IssuingOrderReceiptInfoTrackSearchCriteria GetSearchCriteriaReceived(object isReceived)
        {
            var criteria = new IssuingOrderReceiptInfoTrackSearchCriteria
            {

            };
           
            DateTime? dateStart = null, dateEnd = null;
            if (!string.IsNullOrEmpty(txtReceivingOrderDateStart.Text.Trim()))
            {
                dateStart = DateTime.Parse(txtReceivingOrderDateStart.Text.Trim());
            }
            if (!string.IsNullOrEmpty(txtReceivingOrderDateEnd.Text.Trim()))
            {
                dateEnd = DateTime.Parse(txtReceivingOrderDateEnd.Text.Trim());
            }
            if (dateStart.HasValue || dateEnd.HasValue)
            {
                criteria.ReceivingOrderDate = new Range<DateTime?>(dateStart, dateEnd);
            }
            //客户
            if (!MiscUtilities.IsNullOrEmpty(ddlCustomer.SelectedValue.ToGuid()))
            {
                criteria.CustomerId = ddlCustomer.SelectedValue.ToGuid();
            }
            //承运货物
            if (!string.IsNullOrEmpty(ddlGoodsShippedTypeId.SelectedValue))
            {
                criteria.GoodsShippedTypeId = ddlGoodsShippedTypeId.SelectedValue.ToGuid();
            }
            //线路分类
            if (!MiscUtilities.IsNullOrEmpty(ddlRouteTypeId.SelectedValue.ToGuid()))
            {
                criteria.RouteTypeId = ddlRouteTypeId.SelectedValue.ToGuid();
            }
            if (isReceived != null)
            {
                criteria.IsReceived = Convert.ToBoolean(isReceived);
            }
            return criteria;
        }

        protected void gridOrderTotalList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                var entity = (e.Row.DataItem);

                //var orderId = e.Row.Attributes["Id"] = Convert.ToString(DataBinder.Eval(entity, "Id"));


                //var btnEdit = (LinkButton)e.Row.FindControl("btnEntruncked");
                //if (btnEdit != null)
                //{
                    
                //   // btnEdit.OnClientClick = string.Format("search();");
                //}

                //var btnDetails = (ImageButton)e.Row.FindControl("btnDetails");
                //if (btnDetails != null)
                //{
                //    btnDetails.OnClientClick = string.Format("modifyOrder('{0}');return false;", orderId);
                //}
               

            }
        }
    }
}